<template>
  <div class="common-layout">
    <el-row :gutter="20">
      <el-col :sm="24" :lg="12" style="padding-left: 20px">
        <el-form style="margin-top: 20px" :model="queryParamLeft" ref="queryRef" :inline="true">

          <el-form-item label="书名" prop="ipaddr">
            <el-select style="width: 200px" clearable v-model="queryParamLeft.book" placeholder="书名">
              <el-option v-for="item in 7" :key="item" :label= "`${('book')}${item}`" :value= "`${('book')}${item}`" />
            </el-select>
          </el-form-item>

          <el-form-item label="单元名称" prop="ipaddr">
            <el-select style="width: 200px" clearable v-model="queryParamLeft.unit" placeholder="单元名称">
              <el-option v-for="item in 6" :key="item" :label= "`${('unit')}${item}`" :value= "`${('unit')}${item}`" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="getRecordData">筛选</el-button>
          </el-form-item>
        </el-form>
        <el-row style="font-size: 12px">记录列表</el-row>
        <el-table border :data="wordStudyList" >
          <el-table-column label="书名" width="80" align="center" prop="book" />
          <el-table-column label="单元" width="80" align="center" prop="unit" />
          <el-table-column label="学习个数" align="center" prop="correctNum" />
          <el-table-column label="总个数" align="center" prop="wordNum" />
          <el-table-column label="学习时间" width="180" align="center" prop="createTime" />
          <el-table-column label="完成率" align="center" prop="correctRate" >
            <template #default="scope">
              <span>{{ parseInt(scope.row.correctRate * 100).toFixed(0) }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="用时" align="center" prop="usedTime" />
        </el-table>

        <pagination
            style="margin-right: 20px"
            v-show="size > 0"
            :total="size"
            v-model:page="queryParamLeft.pageNum"
            v-model:limit="queryParamLeft.pageSize"
            @pagination="getRecordData"
        />
      </el-col>
      
      <el-col :sm="24" :lg="12" style="padding-left: 20px;padding-right: 20px">
        <el-form style="margin-top: 20px" :model="queryParam" ref="queryRef" :inline="true">

          <el-form-item label="书名" prop="ipaddr">
            <el-select style="width: 200px" clearable v-model="queryParam.book" placeholder="书名">
              <el-option v-for="item in 7" :key="item" :label= "`${('book')}${item}`" :value= "`${('book')}${item}`" />
            </el-select>
          </el-form-item>

          <el-form-item label="单元名称" prop="ipaddr">
            <el-select style="width: 200px" clearable v-model="queryParam.unit" placeholder="单元名称">
              <el-option v-for="item in 6" :key="item" :label= "`${('unit')}${item}`" :value= "`${('unit')}${item}`" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="searchByUnit">查询</el-button>
          </el-form-item>
        </el-form>
        <el-row style="font-size: 12px">记录详情</el-row>
        <el-table
            border
            v-loading="loading"
            :data="pageData"
            ref="mainTable"
            stripe
        >
          <el-table-column type="selection" v-if="false" width="40" align="center"/>
          <el-table-column label="编号" v-if="false" width="210" prop="_id"/>
          <el-table-column label="书名" width="80" align="center" prop="book"/>
          <el-table-column label="单元" width="80" align="center" prop="unit"/>
          <el-table-column label="单词/词组" align="center" prop="name"/>
          <el-table-column label="词性" align="center" width="80" prop="type"/>
          <el-table-column label="中文" align="center" prop="chinese"/>
          <el-table-column label="学习时间" align="center" width="180" prop="time"/>
        </el-table>

        <pagination
            style="margin-right: 20px"
            v-show="total > 0"
            :total="total"
            v-model:page="queryParam.pageNum"
            v-model:limit="queryParam.pageSize"
            @pagination="getPageData"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script setup >
import {ref} from "vue";
import {listStudyPage} from "@/api/word/index.js";
import {listStudy_record} from "@/api/word/wordRecord.js";
import useUserStore from "@/store/modules/user.js";

const { proxy } = getCurrentInstance()
let queryParam = ref({
  unit: undefined,
  book: undefined,
  pageNum: 1,
  pageSize: 10
})
let queryParamLeft = ref({
  unit: undefined,
  book: undefined,
  userId: useUserStore().id,
  type: 'middle',
  pageNum: 1,
  pageSize: 10
})

let pageData = ref([])
let total = ref(0)
let size = ref(0)
let wordStudyList = ref([])
let loading = false

initPage()

function initPage(){
  queryParam.value.pageNum = 1
  queryParam.value.type = 'middle'
  getRecordData()
  // handleQuery()
}

function searchByUnit(){
  if(queryParam.value.unit === undefined){
    proxy.$modal.msgWarning("请选择单元")
    return
  }
  if (queryParam.value.book === undefined) {
    proxy.$modal.msgWarning("请选择书名")
    return
  }
  proxy.$modal.msg("查询的书名:" + queryParam.value.book +  "  课程名称:" + queryParam.value.unit)
  handleQuery()
}

function handleQuery() {
  getPageData()
  let queryTotal ={
    pageNum: null,
    pageSize: 10,
    type: 'middle',
    unit: queryParam.value.unit,
    book: queryParam.value.book,
  }
  listStudyPage(queryTotal).then((res) => {
    // console.log(res)
    if (res.code === 200) {
      total.value = res.data.length
    }
  })
}

function getRecordData(){
  // console.log(queryParams)
  listStudy_record(queryParamLeft.value).then(res => {
    wordStudyList.value = res.rows
    // console.log(res)
    size.value = res.total
  })
}

function getPageData() {
  // queryParam.value.pageNum = 1
  // console.log(queryParam.value)
  // queryParam.value.pageNum = queryParam.page
  listStudyPage(queryParam.value).then((res) => {
    // console.log(res)
    if (res.code === 200 && res.data !== undefined) {
      pageData.value = res.data
    }
    else {
      pageData.value = []
    }
  })
}
</script>

<style scoped lang="scss">
.header {
  margin-top: 20px;
  margin-left: 20px;
}
</style>
